#include "StdAfx.h"
#include "Joust2Machine.h"

void CJoust2Machine::SetSoundBank(u8 data)
{
int offset;
	
offset = (data & 3) * 0x2000;
offset += ((data & 3) >> 2) * 0x10000;
SoundBankPtr = SoundROM2 + offset + 0x10000;
SoundBankData = data;
}

CJoust2Machine::CJoust2Machine()
{
CPU[2] = new CDead6809();
SoundCPU2 = (CDead6809*)CPU[2];
YM2151 = YM2151Init(0,3579580,44100,this);
ChangeLoadInfo(ROM_CPU3,SoundROM2);
}

CJoust2Machine::~CJoust2Machine()
{
YM2151Shutdown(YM2151);
}

int CJoust2Machine::Init()
{
CWilliams2Machine::Init();
SoundCPU2->Init();
SoundCPU2->SetUser(this);
SoundCPU2->SetOp(ReadSoundCPU2);
SoundCPU2->SetRead(ReadSoundCPU2);
SoundCPU2->SetWrite(WriteSoundCPU2);
#if 0
/* Generic muxing PIA 0, maps to input ports 0/3 and 1; port select is CA2 */
const pia6821_interface williams2_muxed_pia_0_intf =
{
	/*inputs : A/B,CA/B1,CA/B2 */ williams_input_port_0_3_r, input_port_1_r, 0, 0, 0, 0,
	/*outputs: A/B,CA/B2       */ 0, 0, williams_port_select_w, 0,
	/*irqs   : A/B             */ 0, 0
};
/* Joust 2 PIA 1 */
const pia6821_interface joust2_pia_1_intf =
{
	/*inputs : A/B,CA/B1,CA/B2 */ input_port_2_r, 0, 0, 0, 0, 0,
	/*outputs: A/B,CA/B2       */ 0, joust2_snd_cmd_w, joust2_pia_3_cb1_w, pia_2_ca1_w,
	/*irqs   : A/B             */ williams_main_irq, williams_main_irq
};
/* Generic PIA 2, maps to DAC data in and sound IRQs */
const pia6821_interface williams2_snd_pia_intf =
{
	/*inputs : A/B,CA/B1,CA/B2 */ 0, 0, 0, 0, 0, 0,
	/*outputs: A/B,CA/B2       */ pia_1_portb_w, DAC_0_data_w, pia_1_cb1_w, 0,
	/*irqs   : A/B             */ williams_snd_irq, williams_snd_irq
};
#endif
//CONFIGURE_PIAS(williams2_muxed_pia_0_intf, joust2_pia_1_intf, williams2_snd_pia_intf);
//PIA[1]->OutputBFunc = PIA1_OutputB;
//PIA[1]->OutputCA2Func = PIA1_OutputCA2;
return(0);
}

void CJoust2Machine::Reset()
{
CWilliams2Machine::Reset();
YM2151Register = 0;
SetSoundBank(0);
SoundCPU2->Reset();
}

void CJoust2Machine::Line(u32 mainc,u32 soundc,int irq)
{
CWilliams2Machine::Line(mainc,soundc,irq);
SoundCPU2->Execute(123);	//2mhz, 2,000,000 / 70 / 272 = ~123
}

void CJoust2Machine::SaveState(CState *s)
{
CWilliams2Machine::SaveState(s);
}

void CJoust2Machine::LoadState(CState *s)
{
CWilliams2Machine::LoadState(s);
}

/*static ADDRESS_MAP_START( williams_cvsd_map, ADDRESS_SPACE_PROGRAM, 8 )
	AM_RANGE(0x4000, 0x4003) AM_MIRROR(0x1ffc) AM_READWRITE(cvsd_pia_r, cvsd_pia_w)
	AM_RANGE(0x6000, 0x6000) AM_MIRROR(0x07ff) AM_WRITE(hc55516_0_digit_clock_clear_w)
	AM_RANGE(0x6800, 0x6800) AM_MIRROR(0x07ff) AM_WRITE(hc55516_0_clock_set_w)
	AM_RANGE(0x7800, 0x7800) AM_MIRROR(0x07ff) AM_WRITE(cvsd_bank_select_w)
	AM_RANGE(0x8000, 0xffff) AM_ROMBANK(5)
ADDRESS_MAP_END*/

u8 CJoust2Machine::ReadSoundCPU2(void *user,u32 addr)
{
CJoust2Machine *M = (CJoust2Machine*)user;

if(addr < 0x800)
	return(M->SoundRAM2[addr]);
if((addr & 0xE001) == 0x2001)
	return(YM2151ReadStatus(M->YM2151));
if(addr >= 0x8000)
	return(M->SoundBankPtr[addr - 0x8000]);
return(0);
}

void CJoust2Machine::WriteSoundCPU2(void *user,u32 addr,u8 data)
{
CJoust2Machine *M = (CJoust2Machine*)user;

if(addr < 0x800)
	{
	M->SoundRAM2[addr] = data;
	return;
	}
if((addr & 0xE000) == 0x2000)
	{
	if(addr & 1)
		YM2151WriteReg(M->YM2151,M->YM2151Register,data);
	else
		M->YM2151Register = data;
	return;
	}
if((addr & 0xF800) == 0x7800)
	M->SetSoundBank(data);
}

u8 CJoust2Machine::PIA0_InputA_Mux(void *user)
{
CJoust2Machine *M = (CJoust2Machine*)user;

if(M->ActivePort == 0)
	return(M->InputData[0]);
return(M->InputData[3]);
}

void CJoust2Machine::PIA0_OutputCB2_Mux(void *user,u8 data)
{
CJoust2Machine *M = (CJoust2Machine*)user;

M->ActivePort = data;
}
